import request from './request';
import { hashMapConstant, billClassList, incomeClassList } from './constant';

export const testHTTP = () => {
  return request({
    url: "https://api.uomg.com/api/rand.qinghua?format=json",
    method: "get",
  });
};

export const loginReq = (account, password) => {
  return request({
    url: "/users/login",
    method: "post",
    data:{
      account,
      password
    }
  });
};

export const registerReq = (account, password) => {
  return request({
    url: "/users/register",
    method: "post",
    data:{
      account,
      password
    }
  });
};

export const addRecordItem = (userId, timestamp, amount, category, note, income) => {
  return request({
    url: "/transactions/save",
    method: "post",
    data: {
      userId,
      timestamp,
      amount: parseFloat(amount),
      categoryId: income ? 
        hashMapConstant(billClassList, category, 'value', 'id', 20) : 
        hashMapConstant(incomeClassList, category, 'value', 'id', 8),
      note,
      income // True: 支出 False: 收入
    }
  });
};

export const getMonthBillList = (type, timestamp, userId) => {
  let income = undefined;
  switch(type) {
    case 'income': income = false; break;
    case 'expense': income = true; break;
    default: income = ''; break;
  }
  return request({
    url: "/transactions/monthlist",
    method: "post",
    data: {
      income, // True: 支出 False: 收入
      timestamp,
      userId
    }
  });
};

export const deleteBillItem = (bid) => {
  return request({
    url: "/transactions/delete",
    method: "get",
    params: {
      id: bid || undefined,
    }
  });
}

export const getUserInfo = (uid) => {
  return request({
    url: "/users/infor",
    method: "post",
    params: {
      id: uid || undefined,
    }
  });
};

export const changeUserInfo = (userObj) => {
  return request({
    url: "/users/mod",
    method: "post",
    data: {
      id: userObj.uid || undefined,
      name: userObj.nickname || undefined,
      mailbox: userObj.email || undefined,
      password: userObj.password || undefined,
    }
  });
};

export const getTodayInfo = (uid) => {
  return request({
    url: "/transactions/todaytotal",
    method: "get",
    params: {
      id: uid || undefined,
    }
  });
}

export const getMonthInfo = (uid) => {
  return request({
    url: "/transactions/monthtotal",
    method: "post",
    data: {
      userId: uid || undefined,
      timestamp: (new Date).getTime(),
    }
  });
};

export const getMembers = (uid) => {
  return request({
    url: "/users/family/list",
    method: "post",
    params: {
      id1: uid || undefined,
    }
  });
};

export const addMembers = (hostId, memId) => {
  return request({
    url: "/users/family/add",
    method: "post",
    params: {
      id1: hostId || undefined,
      id2: memId || undefined,
    }
  });
};

export const delMembers = (uid) => {
  return request({
    url: "/users/family/delete",
    method: "post",
    params: {
      id1: uid || undefined,
    }
  });
};

export const monthCalculate = (id, time) => {
  return request({
    url: "/transactions/monthtotal",
    method: "post",
    data: {
      userId: id || undefined,
      timestamp: (time).getTime() || (new Date).getTime(),
    }
  });
};

export const yearCalculate = (id, time) => {
  return request({
    url: "/transactions/yeartotal",
    method: "post",
    data: {
      userId: id || undefined,
      timestamp: (time).getTime() || (new Date).getTime(),
    }
  });
};